class TopicsController < ApplicationController
    include AuthenticatedSystemBrowse
  
    before_filter :login_required, :only => [ :new, :create, :edit, :update, :destroy, :reply ]
  
    def index
        conditions = []
        [:user_id, :forum_id].each { |attr| conditions << Topic.send(:sanitize_sql, ["topics.#{attr} = ?", params[attr]]) if params[attr] }
        conditions = conditions.any? ? conditions.collect { |c| "(#{c})" }.join(' AND ') : nil
        
        unless params[:forum_id].nil?
            @forum = Forum.find(params[:forum_id])
            @forum_url = forum_url(@forum)
            if @forum.class == CommunityForum
                @forum_title = @forum.community.title
            else
                @forum_title = @forum.title
            end
        end
        
        @topics = Topic.paginate :conditions => conditions, :include => [:user], :order => "replied_at desc", :page => params[:page]
        render_posts_or_xml
    end
  
    def new
        @forum = Forum.find(params[:forum_id])
        @topic = Topic.new
        if @forum.class == CommunityForum
            @page_title = @forum.community.title + ' - 论坛发言'
        else
            @page_title = @forum.title + ' - 论坛发言'
        end
    end
  
    def create
        @forum = Forum.find(params[:forum_id])
        Topic.transaction do
            @topic  = @forum.topics.build(params[:topic])
            @topic.replied_at = Time.now.utc
            @topic.save!
            current_user.topics << @topic
            current_user.add_scores(2)
        end
	
	respond_to do |format|
            format.html { redirect_to topic_url(@forum, @topic) }
            format.xml  { head :created, :location => formatted_topic_url(:forum_id => @forum, :id => @topic, :format => :xml) }
        end
	
        rescue ActiveRecord::RecordInvalid
        render :action => 'new'
    end
  
    def edit
        @topic = current_user.topics.find(params[:id])
        @page_title = ' - 编辑帖子'
    end
  
    def update
        @topic = current_user.topics.find(params[:id])
	    @topic.update_attributes(params[:topic])
	
	    respond_to do |format|
            format.html { redirect_to topic_url(@topic.forum, @topic) }
            format.xml  { head 200 }
	end
	rescue ActiveRecord::RecordInvalid
        render :action => 'edit'
    end
  
    def destroy
  	@topic = Topic.find(params[:id])
  	if @topic.user_has_auth_destroy?(current_user)
  	    @topic.destroy
  	    flash[:notice] = "删除帖子成功!"
  	    
  	    redirect_to forum_url(@topic.forum)
  	else
  	    raise ActiveRecord::RecordNotFound
  	end
    end
    
    def delete_reply
        topoc_reply = current_user.topic_replies.find(params[:id])
        topoc_reply.destroy
        redirect_to topic_url(topoc_reply.topic.forum, topoc_reply.topic)
    end
    
    def show
  	@topic = Topic.find(params[:id], :include => [{:user => :community}, :forum, :topic_replies ])
  	if @topic.forum.class == CommunityForum
            @page_title = @topic.title + ' - ' + @topic.forum.community.title + ' - 论坛'
  	else
            @page_title = @topic.title + ' - ' + @topic.forum.title + '论坛'
  	end
  	@topic_reply = TopicReply.new
  	@topic_reply.title = "re:" + @topic.title
  	@replies = @topic.topic_replies
  	respond_to do |format|
            format.html { render :action => "show.rhtml" }
            format.rss  { render :action => "show.rxml", :layout => false }
            format.xml  { render :xml => @topic.to_xml }
        end
    end
  
    def reply
        @topic = Topic.find(params[:id], :include => [:forum])
        topic_reply = TopicReply.new(params[:topic_reply])
        Topic.transaction do
            topic_reply.topic_id = @topic.id
            topic_reply.user_id = current_user.id
            topic_reply.save!    
            current_user.add_scores(1)
            @topic.topic_replies_count = @topic.topic_replies_count + 1
            @topic.replied_at = Time.now.utc
            @topic.save!
	end
	redirect_back_or_default topic_url(@topic.forum, @topic)
	rescue ActiveRecord::RecordInvalid
        redirect_back_or_default topic_url(@topic.forum, @topic)
    end
  
  protected
    def render_posts_or_xml(template_name = action_name)
      respond_to do |format|
        format.html { render :action => "#{template_name}.rhtml" }
        format.rss  { render :action => "#{template_name}.rxml", :layout => false }
        format.xml  { render :xml => @topics.to_xml }
      end
    end
end
